﻿package com.firewolf.shiro.authentication.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.realm.Realm;

public class MyRealm2 implements Realm {

	@Override
	public String getName() {
		return "myRealm2";
	}

	@Override
	public boolean supports(AuthenticationToken token) {

		return token instanceof UsernamePasswordToken;
	}

	@Override
	public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

		String name = (String) token.getPrincipal();
		String pwd = new String((char[]) token.getCredentials());
		if (!"zhang".equals(name))
			throw new UnknownAccountException();
		if (!"123".equals(pwd))
			throw new IncorrectCredentialsException();
		return new SimpleAuthenticationInfo(name + "@163.com", pwd, getName());
	}

}
